package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoVO;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-04-07
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Override
    @Transactional
    public String saveCourseInfo(CourseInfoVO courseInfoVO) {
//        拿出数据复制到课程bean类中.

        // 课程表类
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVO, eduCourse);
        if (eduCourse.getSubjectParentId() == null) {
            // 添加
            eduCourse.setSubjectParentId("12");
        }
        boolean saveEduCourse = this.save(eduCourse);
        if (!saveEduCourse) {
            throw new GuliException(20001, "课程课信息添加失败");
        }
//        两个表id是一样的.
        String id = eduCourse.getId();

//        详细类
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVO.getDescription());
        eduCourseDescription.setId(id);
        boolean saveDescription = eduCourseDescriptionService.save(eduCourseDescription);

        if (!saveDescription ) {
            throw new GuliException(20001, "课程详情添加失败");
        }
//        拿出数据复制到课程描述bean类中.
        return id;
    }
}
